using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class TemplateControlDal : Base
    {
        public TemplateControlDal() : base() { }

        public enum Fields
        {
            Id, 
            TemplateId, 
            Name, 
            TypeId,
            TypeName,
            AdminControlType,
            Notes,
            Sequence
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.TemplateId, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<int>(Fields.TypeId, reader, table);
            GetReaderValue<string>(Fields.TypeName, reader, table);
            GetReaderValue<string>(Fields.AdminControlType, reader, table);
            GetReaderValue<string>(Fields.Notes, reader, table);
            GetReaderValue<int>(Fields.Sequence, reader, table);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllByTemplateId(int templateId)
        {
            SqlCommand cmd = CreateCommand("GetAllByTemplateId");
            AddInputParameter(cmd, "TemplateId", templateId, SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public void Update(ref int id, int templateId, string name, int typeId, ref string typeName, ref string adminControlType, string notes, ref int sequence, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "TemplateId", templateId, SqlDbType.Int);
            AddInputParameter(cmd, "Name", name, SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "TypeId", typeId, SqlDbType.Int);
            AddInputParameter(cmd, "Notes", notes, SqlDbType.VarChar, 1000);
            AddInputParameter(cmd, "Sequence", sequence, SqlDbType.Int);
            AddInputParameter(cmd, "VersionId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "TypeNameOut", SqlDbType.VarChar, 50);
            AddOutputParameter(cmd, "AdminControlTypeOut", SqlDbType.VarChar, 255);
            AddOutputParameter(cmd, "SequenceOut", SqlDbType.Int);
            AddOutputParameter(cmd, "VersionIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            typeName = GetOutputValue<string>(cmd, "TypeNameOut");
            adminControlType = GetOutputValue<string>(cmd, "AdminControlTypeOut");
            sequence = GetOutputValue<int>(cmd, "SequenceOut");
            concurencyId= GetOutputValue<byte[]>(cmd, "VersionIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "VersionId", concurencyId, SqlDbType.Timestamp);

            ExecuteCommand(cmd);

        }
    }
}
